package com.itcast.homework05.work09;

import java.util.ArrayList;
import java.util.Comparator;

/*
要写注释哦!!!
*/
public class MyArrays {
    /*泛型让该方法更灵活, 调用者可以传入任何引用类型的数组都支持, 提高了方法扩展性*/

    /**
     * 选择排序
     * @param list
     * @param comparator
     * @param <T>
     */
    public static <T> void sort(ArrayList<T> list, Comparator<T> comparator){
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (comparator.compare(list.get(i), list.get(j)) > 0) {
                    T temp = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, temp);
                }
            }
        }
    }

    /**
     * 冒泡排序
     * @param list
     * @param comparator
     * @param <T>
     */
    public static <T> void sort1(ArrayList<T> list, Comparator<T> comparator){
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size()-i-1; j++) {
                if (comparator.compare(list.get(j), list.get(j+1)) > 0) {
                    T temp = list.get(j);
                    list.set(j, list.get(j+1));
                    list.set(j+1, temp);
                }
            }
        }
    }
}
